#include "Thread.h"
//#include "process.h"

ThreadWorker::ThreadWorker()
{
	m_Identifier = 0;
	m_ThreadName = "";
	m_ThreadWorkBenginEvent = 0;
	m_ThreadWorkFinshedEvent = 0;
}

ThreadWorker::ThreadWorker(char* name)
{
	m_Identifier = 0;
	m_ThreadName = name;
	m_ThreadWorkBenginEvent = 0;
	m_ThreadWorkFinshedEvent = 0;
}

ThreadWorker::~ThreadWorker()
{

}

bool ThreadWorker::Init()
{
	return true;
}

//void ThreadWorker::ThreadWorkerRun()
//{
//	ResumeThread(m_Identifier);
//}

bool ThreadWorker::StartWork()
{
	//ResumeThread(m_Identifier);
	//if(!SetEvent(this->m_ThreadWorkBenginEvent))
	//{
	//	return false;
	//	//handle errer
	//}
	//WaitForMultipleObjects(1,&this->m_Identifier,TRUE,INFINITE);
	return true;//this->OnWorkFinished();
}

bool ThreadWorker::OnInit(void* pParemeter)
{
	return true;
}

bool ThreadWorker::OnWork()
{
	return true;
}

bool ThreadWorker::OnWorkFinished()
{
	return true;
}

void ThreadWorker::OnThreadEvent()
{
	
}

void ThreadWorker::OnThreadState()
{

}

void ThreadWorker::SetState(ThreadWorkerState State)
{
	this->m_State = State;
	this->OnThreadState();
}

void ThreadWorker::AddSignalEvent(ThreadEvent* pEvent)
{
	//m_ThreadWorkerEventVector.push_back(pEvent);
	//ReleaseSemaphore( this->m_ThreadWorkSemaphore, 1, NULL );
}


ThreadControler::ThreadControler()
{

}

ThreadControler::~ThreadControler()
{
}



int ThreadControler::GetSuitableThreadCounts()
{
	return 0;
}


bool ThreadControler::CreateSuitableThreadModle()
{

	return true;
}

bool ThreadControler::Initialize()
{
	CreateSuitableThreadModle();
	return true;
}

int ThreadControler::GetFreeThreadControlObjectIndex()
{
	for(int i = 0; i < m_SuitableThreadCounts; ++i)
	{
		if(m_ThreadControlObject[0] == NULL)
			return i;
	}
	return -1;
}

void ThreadControler::MarkThreadControlObject(int index,void* control_object)
{
	m_ThreadControlObject[index] = control_object;
}

bool ThreadControler::InitThread(ThreadWorker* thread)
{
	if(m_ThreadWorkerMap.size() <= m_SuitableThreadCounts)
		if(thread)
		{
			int free_index = GetFreeThreadControlObjectIndex();
			if(free_index == -1)
				return false;
			MarkThreadControlObject(free_index,thread->m_ThreadWorkFinshedEvent);
			m_ThreadWorkerMap[thread->m_ThreadName] = thread;
			return true;
		}
	return false;
}

bool ThreadControler::RunThreadWorker(std::string worker_name)
{
	THREAD_WORKER_MAP::iterator look_for;
	look_for = m_ThreadWorkerMap.find(worker_name);
	if( look_for == m_ThreadWorkerMap.end())
	{
		return NULL;
	}
	m_ThreadWorkerMap[worker_name]->StartWork();
	return true;
}

bool ThreadControler::StopThreadWorker(std::string worker_name)
{
	THREAD_WORKER_MAP::iterator look_for;
	look_for = m_ThreadWorkerMap.find(worker_name);
	if( look_for == m_ThreadWorkerMap.end())
	{
		return NULL;
	}
	return m_ThreadWorkerMap[worker_name]->StopWork();
}

bool ThreadControler::KillThreadWorker(std::string worker_name)
{
	THREAD_WORKER_MAP::iterator look_for;
	look_for = m_ThreadWorkerMap.find(worker_name);
	if( look_for == m_ThreadWorkerMap.end())
	{
		return NULL;
	}
	return m_ThreadWorkerMap[worker_name]->KillMyself();
}

void ThreadControler::CheckWorkersStateAndHandleEvent()
{
	for_std_unit(THREAD_WORKER_MAP,m_ThreadWorkerMap)
	{
		if(it->second->GetThreadWorkerState() == THREAD_RUNNING_NOTHING)
		{
			it->second->OnThreadState();
		}
		it->second->OnThreadEvent();
	}
}